package com.cisco.vsx.test.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.cisco.vsx.email.EmailEvents;
import com.cisco.vsx.email.EmailTemplates;
import com.cisco.vsx.email.Event;
import com.cisco.vsx.email.IEmailSender;
import com.cisco.vsx.email.Template;
import com.cisco.vsx.email.VSXMailMessage;
import com.cisco.vsx.email.service.IEmailTemplateService;
import com.cisco.vsx.test.RegistrationService;
import com.cisco.vsx.test.vo.User;

/**
 * @author naresred
 * 
 */
public class RegistrationServiceImpl implements RegistrationService {

	@Autowired
	private IEmailSender vsxMailSender;
	
	@Autowired
	private VSXMailMessage mailMessage;
	
	@Autowired
	IEmailTemplateService emailTemplateService;
	
	@Override
	public void register(User user) {
		// TODO Auto-generated method stub
		sendConfirmationEmail(user);
	}

	private void sendConfirmationEmail(final User user) {
		Map<Object, Object> model = new HashMap<Object, Object>();
//		model.put("user.name", user.getName());
//		model.put("user.email", user.getEmail());
		
		model.put("user", user);
		
		mailMessage.setTo(user.getEmail());
		
		// Include header
		mailMessage.setIncludeHeader(true);
		Template headerTemplateCriteria = new Template();
		headerTemplateCriteria.setActive(true);
		headerTemplateCriteria.setDefaultTemplate(true);
		headerTemplateCriteria.setEvent(new Event("Platform_Header"));
		headerTemplateCriteria.setName(EmailTemplates.VSX_HEADER.getTemplateName());
//		headerTemplateCriteria.setLocale("en_US");
		
		// If we want to chose non default template for an event, we need to get the template id some how.
		// There can be a relation table in db to capture customer specific templates for an event
		List<Template> headers = emailTemplateService.getTemplates(headerTemplateCriteria);
		if (headers.size() > 0) {
			// If there are multiple results get default template from the results
			mailMessage.setHeaderTemplateId(headers.get(0).getId());
			// set images
			mailMessage.setImages(headers.get(0).getImages());			
		}		
		
		//Include footer
		mailMessage.setIncludeFooter(true);
		Template footerTemplateCriteria = new Template();
		footerTemplateCriteria.setActive(true);
		footerTemplateCriteria.setDefaultTemplate(true);
		footerTemplateCriteria.setEvent(new Event("Platform Footer"));
		footerTemplateCriteria.setName(EmailTemplates.VSX_FOOTER.getTemplateName());
//		footerTemplateCriteria.setLocale("en_US");
		
		List<Template> footers = emailTemplateService.getTemplates(footerTemplateCriteria);
		if (footers.size() > 0) {
			mailMessage.setFooterTemplateId(footers.get(0).getId());
		}		
		
		// Subject
		Template subjectTemplateCriteria = new Template();
		subjectTemplateCriteria.setActive(true);
		subjectTemplateCriteria.setDefaultTemplate(true);
		subjectTemplateCriteria.setEvent(new Event(EmailEvents.ENGAGEMENT_CREATED.getEventName()));
		subjectTemplateCriteria.setName("Engagement_Created_Subject");
		subjectTemplateCriteria.setLocale("en_US");
		
		List<Template> subjects = emailTemplateService.getTemplates(subjectTemplateCriteria);
		if (subjects.size() > 0) {
			mailMessage.setSubjectTemplateId(subjects.get(0).getId());
		}		
		
		// Body
		Template bodyTemplateCriteria = new Template();
		bodyTemplateCriteria.setActive(true);
		bodyTemplateCriteria.setDefaultTemplate(true);
		bodyTemplateCriteria.setEvent(new Event(EmailEvents.ENGAGEMENT_CREATED.getEventName()));
		bodyTemplateCriteria.setName("Engagement_Created_Body");
		bodyTemplateCriteria.setLocale("en_US");
		
		List<Template> bodies = emailTemplateService.getTemplates(bodyTemplateCriteria);
		if (bodies.size() > 0) {
			mailMessage.setBodyTemplateId(bodies.get(0).getId());
			mailMessage.setAttachments(bodies.get(0).getAttachments());
		}		
		
		mailMessage.setSubjectModel(model);
		mailMessage.setBodyModel(model);
		
		vsxMailSender.send(mailMessage);		
	}

}
